package com.hs.carrental.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hs.carrental.common.Result;
import com.hs.carrental.dto.order.OrderDeleteDto;
import com.hs.carrental.dto.order.OrderPageDto;
import com.hs.carrental.dto.order.OrderSaveDto;
import com.hs.carrental.dto.order.OrderStatDto;
import com.hs.carrental.service.ICarService;
import com.hs.carrental.service.IOrderService;
import com.hs.carrental.util.Helper;
import com.hs.carrental.vo.order.OrderPageVo;
import com.hs.carrental.vo.order.OrderStatChartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/carrental/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICarService carService;

    /**
     * 新增 编辑
     * @param orderSaveDto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody OrderSaveDto orderSaveDto){
        return orderService.save(orderSaveDto)?Result.ok("操作成功"):Result.failed("操作失败");
    }

    /**
     * 删除
     * @param orderDelete
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody OrderDeleteDto orderDelete){
        if (Helper.isEmpty(orderDelete)||Helper.isEmpty(orderDelete.getOrderId())){
            return Result.failed("主键为空");
        }
        //将车辆状态改为未租赁
        return orderService.delete(orderDelete.getOrderId())?Result.ok("操作成功"):Result.failed("操作失败");
    }

    /**
     * 分页查询
     * @param orderPageDto
     * @return
     */
    @PostMapping("/getPage")
    public Result<IPage<OrderPageVo>> getPage(@RequestBody OrderPageDto orderPageDto){
        IPage<OrderPageVo> page = orderService.getPage(orderPageDto);
        return Result.ok(page);
    }

    /**
     * 统计每个门店每个月的订单总量
     * @param orderStatDto
     * @return
     */
    @PostMapping("/stat")
    public Result<Map<String,Object>> stat(@RequestBody OrderStatDto orderStatDto){
        List<OrderStatChartVo> orderStatChartVos = orderService.statOrderByStore(orderStatDto.getStoreId(), orderStatDto.getYear());
        Map<String,Object> res = new HashMap<>();
        res.put("chart",orderStatChartVos);
        return Result.ok(res);
    }
}
